package com.zhm.demo.redis;


import com.hthium.json.util.ValueWrapper;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.data.redis.connection.*;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;
import java.util.Arrays;

public class RedisTemplateUtil {

    public static RedisSerializer stringRedisSerializer =  new  StringRedisSerializer();

    public static LettuceConnectionFactory  makeLocalConnect(Integer db){
        GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
        genericObjectPoolConfig.setMaxIdle(8);
        genericObjectPoolConfig.setMinIdle(0);
        genericObjectPoolConfig.setMaxTotal(8);
        genericObjectPoolConfig.setMaxWaitMillis(1);
        genericObjectPoolConfig.setTimeBetweenEvictionRunsMillis(100);
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setDatabase(db);
        redisStandaloneConfiguration.setHostName("127.0.0.1");
        redisStandaloneConfiguration.setPort(6379);
//        redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
        LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
                .commandTimeout(Duration.ofMillis(2))
                .shutdownTimeout(Duration.ofMillis(20))
                .poolConfig(genericObjectPoolConfig)
                .build();

        LettuceConnectionFactory factory = new LettuceConnectionFactory(redisStandaloneConfiguration, clientConfig);
        factory.afterPropertiesSet();
//        factory.setShareNativeConnection(true);
//        factory.setValidateConnection(false);
        return factory;
    }

    public static GenericObjectPoolConfig genericObjectPoolConfig(){
        GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
        genericObjectPoolConfig.setMaxIdle(10);
        genericObjectPoolConfig.setMinIdle(8);
        genericObjectPoolConfig.setMaxTotal(20);
        genericObjectPoolConfig.setMaxWaitMillis(1);
        genericObjectPoolConfig.setTimeBetweenEvictionRunsMillis(600);
        return genericObjectPoolConfig;
    }

    public static LettuceConnectionFactory  makeSitConnect(Integer db){
        GenericObjectPoolConfig genericObjectPoolConfig = genericObjectPoolConfig();


        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setDatabase(db);
        redisStandaloneConfiguration.setHostName("172.24.73.24");
        redisStandaloneConfiguration.setPort(6379);
//        redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
        LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
                .commandTimeout(Duration.ofSeconds(2))
                .shutdownTimeout(Duration.ofMillis(20))
                .poolConfig(genericObjectPoolConfig)
                .build();

        LettuceConnectionFactory factory = new LettuceConnectionFactory(redisStandaloneConfiguration, clientConfig);
        factory.afterPropertiesSet();
//        factory.setShareNativeConnection(true);
//        factory.setValidateConnection(false);
        return factory;
    }

    public static LettuceConnectionFactory  makePreConnect(Integer db){

        GenericObjectPoolConfig genericObjectPoolConfig = genericObjectPoolConfig();

        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setDatabase(db);
        redisStandaloneConfiguration.setHostName("172.24.73.28");
//        redisStandaloneConfiguration.setPort(6379);
//        redisStandaloneConfiguration.setPassword(RedisPassword.of(""));
        LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
                .commandTimeout(Duration.ofSeconds(20))
                .shutdownTimeout(Duration.ofSeconds(200))
                .poolConfig(genericObjectPoolConfig)
                .build();

        LettuceConnectionFactory factory = new LettuceConnectionFactory(redisStandaloneConfiguration, clientConfig);
        factory.afterPropertiesSet();
        factory.setShareNativeConnection(true);
        factory.setValidateConnection(false);
        return factory;
    }

    public static LettuceConnectionFactory makeTestConnect(Integer db) {
        String nodes = "172.24.73.29:26379,172.24.73.29:26380,172.24.73.29:26381";
        String masterName="mymaster";
        String password="hcredis1005";

        GenericObjectPoolConfig genericObjectPoolConfig = genericObjectPoolConfig();

        RedisSentinelConfiguration sentinelConfiguration = new RedisSentinelConfiguration();
        sentinelConfiguration.setDatabase(db);
        sentinelConfiguration.setSentinels(Arrays.stream(nodes.split(",")).map(node-> RedisNode.fromString(node)).toList());
        sentinelConfiguration.setMaster(masterName);
        sentinelConfiguration.setPassword(password);

        LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
                .commandTimeout(Duration.ofSeconds(20))
                .shutdownTimeout(Duration.ofSeconds(200))
                .poolConfig(genericObjectPoolConfig)
                .build();


        LettuceConnectionFactory factory = new LettuceConnectionFactory(sentinelConfiguration, clientConfig);
        factory.afterPropertiesSet();
        factory.setShareNativeConnection(true);
        factory.setValidateConnection(false);
        return factory;
    }


//    public static LettuceConnectionFactory makeProdConnect(Integer db) {
//        String nodes = "106.54.204.64:";
//        String masterName="mymaster";
//        String password="hcredis1005";
//
//        GenericObjectPoolConfig genericObjectPoolConfig = genericObjectPoolConfig();
//
//        RedisSentinelConfiguration sentinelConfiguration = new RedisSentinelConfiguration();
//        sentinelConfiguration.setDatabase(db);
//        sentinelConfiguration.setSentinels(Arrays.stream(nodes.split(",")).map(node-> RedisNode.fromString(node)).toList());
//        sentinelConfiguration.setMaster(masterName);
//        sentinelConfiguration.setPassword(password);
//
//        LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder()
//                .commandTimeout(Duration.ofSeconds(20))
//                .shutdownTimeout(Duration.ofSeconds(200))
//                .poolConfig(genericObjectPoolConfig)
//                .build();
//
//
//        LettuceConnectionFactory factory = new LettuceConnectionFactory(sentinelConfiguration, clientConfig);
//        factory.afterPropertiesSet();
//        factory.setShareNativeConnection(true);
//        factory.setValidateConnection(false);
//        return factory;
//    }

    public static RedisTemplate<String,String> makeStringRedisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
        redisTemplate.setHashKeySerializer(stringRedisSerializer); // Hash value序列化
        redisTemplate.setHashValueSerializer(stringRedisSerializer);
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setValueSerializer(stringRedisSerializer);
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;

    }
    public static RedisTemplate<String, ValueWrapper> makeValueRedisTemplate(RedisConnectionFactory redisConnectionFactory){
        return new ValueWrapperRedisTemplate(redisConnectionFactory);

    }



}
